package org.jxht.web.api;

import org.jxht.dto.entity.ResponseCourseEntity;
import org.jxht.dto.StuCouDto;
import org.jxht.service.CourseService;
import org.jxht.service.StuCouService;
import org.jxht.util.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by huang on 2017/3/20 0020.
 */
@Controller
@RequestMapping("JxhtWeb/StuCouController")
public class StuCouController {

    @Autowired
    private StuCouService stuCouService;

    @Autowired
    private CourseService courseService;

    /**
     * 学生选课
     *
     * @param requestId 学生学号
     * @param teaCouId  教师课程记录号
     * @return String
     */
    @RequestMapping(value = "/{requestId}/insertStuCouRecord")
    @ResponseBody
    public String insertStuCouRecord(@PathVariable(value = "requestId") int requestId,
                                     @RequestParam(value = "teaCouId") int teaCouId) {
        StuCouDto dto = new StuCouDto(requestId, teaCouId);
        return stuCouService.insertStuCouService(dto);
    }

    @RequestMapping(value = "/{requestId}/querySelectCouRecordSigle", method = RequestMethod.GET)
    @ResponseBody
    public String querySelectCouRecordSigle(@PathVariable(value = "requestId") int requestId,
                                            @RequestParam(value = "teaCouId") int teaCouId) {
        ResponseCourseEntity entity = new ResponseCourseEntity();
        entity.setCouProperty("公共选修");
        entity.setStuId(requestId);
        entity.setTeaCouId(teaCouId);
        List<ResponseCourseEntity> responseCourseEntitys = stuCouService.queryselectCouRecord(entity);
        if (responseCourseEntitys.size() > 0)
            return "SUCCESS";
        else
            return "FAILURE";
    }

    @RequestMapping(value = "/{requestId}/querySelectCouRecord")
    @ResponseBody
    public List<ResponseCourseEntity> querySelectCouRecord(@PathVariable(value = "requestId") int requestId) {
        ResponseCourseEntity entity = new ResponseCourseEntity();
        entity.setCouProperty("公共选修");
        entity.setStuId(requestId);
        return stuCouService.queryselectCouRecord(entity);
    }

    /**
     * 更新学生成绩
     *
     * @param requestJson Json字符串
     * @return
     */
    @RequestMapping(value = "/changeStuCouRecord")
    @ResponseBody
    public String changeStuCouRecord(@RequestParam(value = "requestJson") String requestJson) {
        List<StuCouDto> dtos = GsonUtil.parseJsonArrayWithGson(requestJson, StuCouDto.class);
        for (StuCouDto dto : dtos) {
            stuCouService.updataStuCouService(dto);
        }
        return "SUCCESS";
    }

    /**
     * 获取学生成绩
     *
     * @param requestId  学号、课程号、教师号、专业号
     * @param requestWay 请求方
     * @return List<ResponseCourseEntity>
     */
    @RequestMapping(value = "/{requestId}/getStuScore", method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<ResponseCourseEntity> getStuScore(@PathVariable(value = "requestId") int requestId,
                                                  @RequestParam(value = "requestWay") String requestWay) {
        List lists = stuCouService.queryStuScore(requestId, requestWay);
        return lists;
    }

    /**
     * 学生重修申请
     *
     * @param reqeuestId 学生请求Id
     * @param teaCouId   教师课程号
     * @return String
     */
    @RequestMapping(value = "/{requestId}/renewalApplication")
    @ResponseBody
    public String renewalApplication(@PathVariable(value = "requestId") int reqeuestId,
                                     @RequestParam(value = "teaCouId") int teaCouId) {
        if (0 != stuCouService.RenewalApplicationCourse(new StuCouDto(reqeuestId, teaCouId))) {
            return "SUCCESS";
        } else
            return "FAIL";
    }

    /**
     * 学生退课
     *
     * @param requestId 学生学号
     * @param teaCouId  教师课程记录号
     * @return String
     */
    @RequestMapping(value = "/{requestId}/deleteStuCouRecord")
    @ResponseBody
    public String deleteStuCouRecord(@PathVariable(value = "requestId") int requestId,
                                     @RequestParam(value = "teaCouId") int teaCouId) {
        StuCouDto dto = new StuCouDto(requestId, teaCouId);
        stuCouService.deleteStuCouService(dto);
        return "SUCCESS";
    }
}
